package com.edu.servlet;

import com.edu.bean.Feedback;
import com.edu.service.AdminFeedbackService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.edu.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/feedback/*")
public class AdminFeedbackServlet extends HttpServlet {
    private AdminFeedbackService adminFeedbackService = new AdminFeedbackService();
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        if (pathInfo == null) {
            pathInfo = "/";
        }

        switch (pathInfo) {
            case "/list":
                listFeedback(request, response);
                break;
            case "/detail":
                getFeedbackDetail(request, response);
                break;
            default:
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }

    private void listFeedback(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取分页参数
        String pageStr = request.getParameter("page");
        String title = request.getParameter("title");

        int currentPage = 1;
        if (pageStr != null && !pageStr.trim().isEmpty()) {
            try {
                currentPage = Integer.parseInt(pageStr);
                if (currentPage < 1) {
                    currentPage = 1;
                }
            } catch (NumberFormatException e) {
                // 使用默认值 1
            }
        }

        int pageSize = 10;

        // 获取总记录数和分页数据
        int totalCount = adminFeedbackService.getFeedbackCount(title);
        int totalPages = (totalCount + pageSize - 1) / pageSize;

        // 确保当前页不超过总页数
        if (currentPage > totalPages && totalPages > 0) {
            currentPage = totalPages;
        }

        List<Feedback> feedbackList = adminFeedbackService.getFeedbackByPage(title, currentPage, pageSize);

        // 设置请求属性
        request.setAttribute("feedbackList", feedbackList);
        request.setAttribute("currentPage", currentPage);
        request.setAttribute("totalPages", totalPages);
        request.setAttribute("totalCount", totalCount);
        request.setAttribute("title", title);

        // 转发到列表页面
        request.getRequestDispatcher("/admin/feedback-list.jsp").forward(request, response);
    }

    private void getFeedbackDetail(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String idStr = request.getParameter("id");
        Map<String, Object> result = new HashMap<>();

        try {
            int feedbackId = Integer.parseInt(idStr);
            Feedback feedback = adminFeedbackService.getFeedbackById(feedbackId);

            if (feedback != null) {
                result.put("success", true);
                result.put("data", feedback);
            } else {
                result.put("success", false);
                result.put("message", "反馈不存在");
            }
        } catch (NumberFormatException e) {
            result.put("success", false);
            result.put("message", "参数错误");
        }

        response.setContentType("application/json;charset=utf-8");
        objectMapper.writeValue(response.getWriter(), result);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}